<?php
namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Session;
use think\Config;
use think\Db;
use think\Loader;
use think\Cookie;
use app\common\jy\geetestLib as smjy;
// use vendor\Gregwar\Captcha\CaptchaBuilder;

class Login extends Controller
{
    public function index()
    {
        // return $this->fetch();
        // 注册页面
        return view('index@Login/index',[
            'title' => '注册用户'
        ]);
    }

    public function firstSmjy()
    {
        $GtSdk = new smjy(CAPTCHA_ID, PRIVATE_KEY);

        $data = array(
                "user_id" => "test", # 网站用户id
                "client_type" => "web", #web:电脑上的浏览器；h5:手机上的浏览器，包括移动应用内完全内置的web_view；native：通过原生SDK植入APP应用的方式
                "ip_address" => "127.0.0.1" # 请在此处传输用户请求验证时所携带的IP
            );

        $status = $GtSdk->pre_process($data, 1);
        // $_SESSION['gtserver'] = $status;
        // $_SESSION['user_id'] = $data['user_id'];
        Session::set('gtserver', $status);
        Session::set('user_id', $data['user_id']);
        echo $GtSdk->get_response_str();
    }

    /**
     * 登录验证
     * @param Request $request
     * @return string
     * @throws \think\exception\DbException
     */
    public function dologin(Request $request)
    {
        
        $res = [
            'email' => $_POST['email'],
            'password' => md5($_POST['password'])
        ];
        // dump($_POST);die;
        $list = db('h_user')->where($res)->where('status', '1')->find();
        // 判断结果
        if($list){
            //是否记住密码  存session 和 cookie
            Session::set('id', $list['id']);
            Session::set('name', $list['name']);
            Session::set('password', $list['password']);
            Session::set('email', $list['email']);
            Session::set('phone', $list['phone']);
            if(isset($_POST['remember'])){
                Cookie::set('id', $list['id'], 3600*24*7);
                Cookie::set('name', $list['name'], 3600*24*7);
                Cookie::set('password', $list['password'], 3600*24*7);
                Cookie::set('email', $list['email'], 3600*24*7);
                Cookie::set('phone', $list['phone'], 3600*24*7);
            }
            return $list;
        }else{
            return false;
        }

    }

    //电话登录
    public function smslogin(Request $request)
    {
        $res = $_POST;
       // dump(session::get());die;
       if (session::get('verify')) {
        if ($res['form'] == session::get('verify')) {
            $list = Db::name('h_user')
                    ->where('phone',$res['phone'])
                    ->where('status','1')
                    ->find();
//            dump($list);die;
            if ($list) {

                //是否记住密码  存session 和 cookie
                Session::set('id', $list['id']);
                Session::set('name', $list['name']);
                Session::set('password', $list['password']);
                Session::set('email', $list['email']);
                Session::set('phone', $list['phone']);
                if(!empty($res['remember'])){
                    Cookie::set('id', $list['id'], 3600*24*7);
                    Cookie::set('name', $list['name'], 3600*24*7);
                    Cookie::set('password', $list['password'], 3600*24*7);
                    Cookie::set('email', $list['email'], 3600*24*7);
                    Cookie::set('phone', $list['phone'], 3600*24*7);
                }else{}
//                var_dump($list);die;
                return $list;

            }else{
                 return false;
            }
          }else{
                return false;
          }
    }else{
        return false;
}
    }
    /**
     * @return string
     */
    public function logOut()
    {
        //读取session的值
        $res = Session::get();
        // 删除session
        session_unset($res);
        // 删除 存储的cookie
        Cookie::delete('id');
        Cookie::delete('name');
        Cookie::delete('email');
        Cookie::delete('phone');
        Cookie::delete('password');
        return $this->redirect('index/Index/index');

    }

    public function sms()
    {
        // 注册页面
        return view('index@Login/sms',[
            'title' => '注册用户'
        ]);
    }

    public function StartCaptchaServlet() {
        $GtSdk = new GeetestLib($this->config['geetest_id'], $this->config['geetest_key']);
        $status = $GtSdk->pre_process($this->param, 1);
        Session::set('gtserver', $status);
        Session::set('user_id', 1);
        echo $GtSdk->get_response_str();
    }
    
    public function check() {
        $GtSdk = new GeetestLib($this->config['geetest_id'], $this->config['geetest_key']);
        $param = input('post.');
        if (Session::get('gtserver') == 1) {
            echo $GtSdk->success_validate($param['geetest_challenge'], $param['geetest_validate'], $param['geetest_seccode'], $this->param);
        } else { //宕机
            echo $GtSdk->fail_validate($param['geetest_challenge'], $param['geetest_validate'], $param['geetest_seccode']);
        }
    }


}